package com.aounhui.customer.controller;

import com.aounhui.common.entity.Result;
import com.aounhui.common.enums.ResponseEnums;
import com.aounhui.common.exceptions.CreateException;
import com.aounhui.customer.entity.User;
import com.aounhui.customer.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/customer/user/get")
    public Result getUser(@RequestParam("loginName") String loginName) {
        try {
            return userService.getUsers(loginName);
        } catch (Exception e) {
            log.error("查询失败", e);
            e.printStackTrace();
            return Result.error("查询用户发生异常，查询失败");
        }
    }

    @PostMapping(value = "/customer/user/regist")
    public Result userRegister(@RequestBody User user) {
        try {
            return userService.userRegister(user);
        }catch (CreateException e) {
            e.printStackTrace();
            return Result.error(ResponseEnums.ERROR01.getResponseMessage());
        }
    }

    @PostMapping(value = "/customer/user/login")
    public Result userLogin(@RequestBody User user) {
        try {
            System.out.println(user);
            return userService.userLogin(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("用户登陆发生异常，登陆失败");
        }
    }
}
